package opt

import (
	"os"
	path "path/filepath"
	"strings"
)

var (
	Invoked     = os.Args[0]
	BaseInvoked = path.Base(Invoked)
	Flags       = make(map[rune]bool)
	Opts        = make(map[rune]string)

	Args []string
)

func Parse(flags string, opts string, unrecognizedAsArgs bool) {
	i := -1 // only use if os.Args[1:] is empty
	var a string
	for i, a = range os.Args[1:] {
		if a[0] != '-' {
			goto nomore
		}
		if len(a) == 2 && a[1] == '-' {
			break
		}
		for _, r := range a[1:] {
			if strings.ContainsRune(flags, r) {
				Flags[r] = true
			} else if strings.ContainsRune(opts, r) {
				// TODO
			} else if unrecognizedAsArgs {
				goto nomore
			} else {
				panic("unknown option: " + string(r))
			}
		}
	}
	i++
nomore:
	Args = os.Args[i+1:]
}
